// pages/forget/password.js
import {
  request,
  setToken,
  saveFormId
} from '../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectedInputIndex: 1,
    codeInterval: 60,
    codeTimerId: null,
  },
  phone: null,
  toPage(e) {
    let { url, type } = e.currentTarget.dataset
    if (type == 'back') {
      wx.navigateBack()
    } else if (type == 'tab') {
      wx.switchTab({
        url: url,
      })
    } else if (type == 'redirect') {
      wx.redirectTo({
        url: url,
      })
    } else {
      wx.navigateTo({
        url: url,
      })
    }
  },
  inputClick(e){
    let { index } = e.currentTarget.dataset
    this.setData({
      selectedInputIndex: index
    })
  },
 
  getCode() {
    if (!this.phone || this.phone.length != 11) {
      wx.showToast({
        icon: 'none',
        title: '请填写11位手机',
      })
      return
    }
    this.setData({
      codeInterval: 60,
    })
    if (this.data.codeTimerId) {
      clearInterval(this.data.codeTimerId)
    }
    this.data.codeTimerId = setInterval(() => {
      if (this.data.codeInterval <= 0) {
        clearInterval(this.data.codeTimerId)
      }
      this.setData({
        codeInterval: --this.data.codeInterval,
      })
    }, 1000)
    wx.setStorage({
      key: 'get_code_time',
      data: new Date()
    })
    request({
      url: 'Login/verificationCode',
      data: {
        phone: this.phone
      },

      method: 'POST',

      success: data => {
       
        wx.showToast({
          icon: "none",
          title: '获取验证码成功',
        })
      }
    })
  },
  setPhone(e) {
    let {
      value
    } = e.detail

    this.phone = value
  },
  checkCodeInterval() {
    wx.getStorage({
      key: 'get_code_time',
      success: res => {
        if (!res.data) {
            this.setData({
                codeInterval: 0
            })
            return
        }
        let time = parseInt(res.data.getTime() / 1000)
        let now = parseInt(new Date().getTime() / 1000)
        this.setData({
          codeInterval: 60 - (now - time),
        })
        if (this.data.codeTimerId) {
          clearInterval(this.data.codeTimerId)
        }
        this.data.codeTimerId = setInterval(() => {
          if (this.data.codeInterval <= 0) {
            clearInterval(this.data.codeTimerId)
          }
          this.setData({
            codeInterval: --this.data.codeInterval,
          })
        }, 1000)
      },
      fail: err => {
        this.setData({
          codeInterval: 0
        })
      }
    })
  },
  updatePassword(e) {
    let { value, formId } = e.detail
      if (!value.phone || value.phone.length != 11) {
        wx.showToast({
          icon: 'none',
          title: '请填写11位手机',
        })
        return
      }
      if (!value.password) {
        wx.showToast({
          icon: 'none',
          title: '请填写密码',
        })
        return
      }
      if (!value.code) {
        wx.showToast({
          icon: 'none',
          title: '请填写验证码',
        })
        return
      }
      request({
        data: value,
        url: 'Login/getBackPassword',
        method: 'POST',
        success: data => {
          // setToken(data.access_token)
          wx.navigateBack()
        }
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.checkCodeInterval()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})